//
//  LYCollectionviewOnTableviewCell.m
//  音伴尔家听宝
//
//  Created by Burgess on 2017/5/2.
//  Copyright © 2017年 Burgess. All rights reserved.
//

#import "LYCollectionviewOnTableviewCell.h"
#import "LYCollectionCell.h"
#import <UIButton+WebCache.h>
#import "LYClassInfoViewController.h"


@interface LYCollectionviewOnTableviewCell() 

@end

@implementation LYCollectionviewOnTableviewCell

-(void)setvaluetoarraymodel:(NSArray *)array tableviewcell:(LYCollectionviewOnTableviewCell *)cell viewcontroller:(UIViewController *)vc cellIndex:(NSInteger ) index{
    cell.arraymodel = array;
    cell.vc = vc;
    [cell.collectionview reloadData];
    cell.collectionview.tag   =index;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier null:(NSString *)null {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self    = [[[NSBundle mainBundle]loadNibNamed:@"LYCollectionviewOnTableviewCell" owner:NULL options:NULL]firstObject];
        [self setValue:reuseIdentifier forKey:@"reuseIdentifier"];
        CGRect rect                                                = CGRectMake(0, 29, SCREEN_WIDTH, (SCREEN_WIDTH-44)/3+35+20);
        [self comment:rect];

    }
    return self;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        CGRect rect                                        = CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_WIDTH-44)/3+35+20);
        [self comment:rect];
    }
    return self;
}

-(void)comment:(CGRect ) rect{
    UICollectionViewFlowLayout * flow   = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize                                       = CGSizeMake((SCREEN_WIDTH-44)/3, (SCREEN_WIDTH-44)/3+35);
    self.collectionview                              = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:flow];
    self.collectionview.dataSource          = self;
    self.collectionview.delegate               = self;
    self.collectionview.bounces               = NO;
    self.collectionview.backgroundColor = [UIColor whiteColor];
    [self.collectionview registerClass:[LYCollectionCell class] forCellWithReuseIdentifier:@"cellname"];
    [self addSubview:self.collectionview];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark - <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    return self.arraymodel.count;
    if (self.arraymodel.count == 0) {
        return 0;
    }
    return 3;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
        LYCollectionCell  * cell11 = [self.collectionview dequeueReusableCellWithReuseIdentifier:@"cellname" forIndexPath:indexPath];
    
        if(cell11 == NULL){
            cell11 = [[[NSBundle mainBundle]loadNibNamed:@"LYCorseCell" owner:NULL options:NULL]firstObject];
            [cell11 setValue:@"cellname" forKey:@"reuseIdentifier"];
        }

        LYmainPageModel  * models = [[LYmainPageModel alloc]init];
        models = self.arraymodel[indexPath.item];
        
        [cell11.corseBtn sd_setImageWithURL:(NSURL *)models.mob_large_img forState:UIControlStateNormal];
        [cell11.corseBtn setTitle:models.name forState:UIControlStateNormal];
        [cell11.corseBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [cell11.corseBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        cell11.corseBtn.userInteractionEnabled = NO;
        return cell11;
}

//两个cell之间的间距（同一行的cell的间距）
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
        return 10;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(10, 11, 5, 11);
}


#pragma mark - <UICollectionViewDataSource>

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LYClassInfoViewController * two = [[LYClassInfoViewController alloc]init];
    
    LYmainPageModel  * models = [[LYmainPageModel alloc]init];
    models = self.arraymodel[indexPath.item];
    two.courseId = models.course_id;
    two.title         = models.name;
    
    [self.vc.navigationController pushViewController:two animated:YES];
    NSLog(@"%ld - %ld",indexPath.item,(long)collectionView.tag);
}



@end
